<template>
  <a-modal
    title="操作"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="所属公司">
          <a-select
            @change="handleProvinceChange"
            v-decorator="[ 'companyId', {rules: [{ required: true, message: '请选择所属公司' }],initialValue: defaultCompany}]"
            placeholder="请选择所属公司">
            <a-select-option :value="company.id" v-for="company in companyList" :key="company.id">
              {{company.name}}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="默认积分配置"
        >
          <a-input
            placeholder="默认积分配置"
            v-decorator="['defaultPoint', {rules: [{ required: true, message: '请输入。。。' }]}]"
          ></a-input>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="有效时间">
          <a-range-picker style = "width: 100%"
                          :format="dateFormat"
                          v-decorator="[ 'validTime', {rules: [{ required: true, message: '请选择时间' }]}]"
          />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="签到积分发放配置"

        >
          <div v-for="(item,i) in items" >
            <a-input-group   v-decorator="[ 'setNmuber', {rules: [{ required: true,  message: '请输入数字类型',pattern: new RegExp(/^[1-9]\d*$/,'g'), }]}]" >
              <a-col :span="9">
                <a-input placeholder="时间" v-model="item.day"/>
              </a-col>
              <a-col :span="1" style="margin-top: 3px">
                天
              </a-col>
              <a-col :span="10"      >
                <a-input v-model='item.points' placeholder="积分数量" />
              </a-col>
              <a-col :span="1" style="margin-top: 3px">
                分
              </a-col>
              <a-col :span="2">
                <a-button type="danger" @click="del(i)">删除</a-button>
              </a-col>
            </a-input-group>
          </div>
          <a-button @click="onAdd" type="primary" style="margin-top: 10px">添加</a-button>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { save,selectDetailBySignConfigId } from '@/api/market/sign'
  import { selectAll } from '@/api/companyService/company'
  import pick from 'lodash.pick'
  import moment from 'moment';

  export default {
    //--############  data() ###############-->
    data() {
      return {

        dateFormat: 'YYYY-MM-DD',
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        mdl: {},
        form: this.$form.createForm(this),
        isDisabled: false,
        defaultCompany: 1,
        companyList: [],
        validTime:[],
        config:[],
        items: []
      }
    },
    //--############  data() ###############-->

    created() {
      // this.getAllMerchant()
      selectAll().then(res => {
        this.companyList = res.result
        this.defaultCompany = this.companyList[0].id
      })
    },

    //--############  methods() ###############-->
    methods: {

      handleProvinceChange:function(e){
        /*通过商圈id查询该商圈下的商品*/
        /*console.log('handleProvinceChange',e);
        selectProductListByCompanyId(e).then(res => {
          this.defaultConfig = res.result
          console.log(this.defaultConfig);
        })

        this.items=[]*/
      },
      onAdd() {
        let cope = {
          day:'',
          points:''
        }
        this.items.push(cope);
      },
      del: function(index) {
        this.items.splice(index, 1)
      },
      onchange(e){
        console.log(e);
      },
      add() {
        this.isDisabled = false
        this.visible = true
      },
      edit(record) {
        /*查询所有的可以用的配置*/
        this.isDisabled = true
        this.validTime.push(moment(record.beginTime))
        this.validTime.push(moment(record.endTime))
        record.validTime = this.validTime
        this.mdl = Object.assign({}, record)
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(record, 'defaultPoint','validTime','companyId'))
        })
        /*配置修改，查询数据库中数据。*/
        selectDetailBySignConfigId(record.id).then(res => {
          this.items = res.result
        })
      },
      close() {
        this.$emit('close')
        this.visible = false
        this.form.resetFields()
        this.mdl={}
        this.config=[]
        this.items = []
        this.validTime = []
      },
      handleOk() {
        const _this = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          // 验证表单没错误
          if (!err) {
            values.beginTime = values.validTime[0].format('YYYY-MM-DD')+" 00:00:00"
            values.endTime = values.validTime[1].format('YYYY-MM-DD')+" 23:59:59"
            values.id = this.mdl.id;
            _this.confirmLoading = true
            values.signConfigDetailList = _this.items
            // 模拟后端请求 2000 毫秒延迟
            save(values).then((res) => {
              if (res.code === 200) {
                _this.$message.success('保存成功')
                _this.$emit('ok')
                _this.close()
              }else {
                this.$notification.error({
                  message: '错误',
                  description: res.message,
                  duration: 4
                })
              }
            }).catch(() => {
              // Do something
            }).finally(() => {
              _this.confirmLoading = false
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },
    }
    //--############  methods() ###############-->
  }
</script>

<style scoped>

</style>
